<!--
 * @description  : 市管-考核计划检查
 * @descriptionDetail: 市管-考核计划检查
 * @copyright    : 浙江烟草
 * @author       : mc
 * @create       : 2022-08-09 14:43:02
-->
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
    </theme-navbar>
    <!-- #endif -->
    <view class="content u-page pb-60">
      <form-section title="考核计划信息" :customStyle="customStyle"></form-section>
      <u-gap :height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
      <view class="info-lay">
        <u-row>
          <u-col span="7">
            <view class="info-title">
              考核计划名称
            </view>
            <view class="info-value">
              {{ objInfo.planName }}
            </view>
          </u-col>
          <u-col span="5">
            <view class="info-title">
              被考核单位
            </view>
            <view class="info-value">
              {{ objInfo.orgName }}
            </view>
          </u-col>
        </u-row>
        <u-row style="align-items: baseline;">
          <u-col span="7">
            <view class="info-title">
              企业字号
            </view>
            <view class="info-value">
              {{ objInfo.companyName || '/' }}
            </view>
          </u-col>
          <u-col span="5">
            <view class="info-title">
              主体类型
            </view>
            <view class="info-value">
              {{ $u.dic.getDicNameByKeyCode($u.dic.DIC_KEYS.L_SUBJECT_TYPE,objInfo.taskSubject) || '/' }}
            </view>
          </u-col>
        </u-row>
        <u-row style="align-items: baseline;">
          <u-col span="7">
            <view class="info-title">
              经营地址
            </view>
            <view class="info-value">
              {{ objInfo.businessAddr || '/' }}
            </view>
          </u-col>
          <u-col span="5">
            <view class="info-title">
              负责人
            </view>
            <view class="info-value">
              {{ objInfo.managerName || '/' }}
            </view>
          </u-col>
        </u-row>
        <u-row>
          <u-col span="7">
            <view class="info-title">
              考核方式
            </view>
            <view class="sp-info-value">
              <z-dict-radio :dictType="$u.dic.DIC_KEYS.M_CHECK_MODE" :extFilterFun="extFilterFun" v-model="objInfo.checkMode"></z-dict-radio>
            </view>
          </u-col>
        </u-row>
      </view>
      <u-gap :height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
      <form-section title="检查项目" :customStyle="customStyle"></form-section>
      <view class="info-lay">
        <u-form-item labelWidth="250" label="是否公开摆卖" label-position="left">
          <view class="switch-lay">
            <u-switch v-model="objInfo.isPublicSale"></u-switch>
          </view>
        </u-form-item>
        <u-form-item v-show="objInfo.checkMode != M_CH_MODE.SECRET_VISIT" labelWidth="250" label="是否售假" label-position="left">
          <view class="switch-lay">
            <u-switch v-model="objInfo.isFakeSmoke"></u-switch>
          </view>
        </u-form-item>
        <u-form-item v-show="objInfo.checkMode != M_CH_MODE.SECRET_VISIT" labelWidth="250" label="是否售私" label-position="left">
          <view class="switch-lay">
            <u-switch v-model="objInfo.isSmuggleSmoke"></u-switch>
          </view>
        </u-form-item>
        <u-form-item v-show="objInfo.checkMode != M_CH_MODE.SECRET_VISIT" labelWidth="250" label="是否售非" label-position="left">
          <view class="switch-lay">
            <u-switch v-model="objInfo.isFfltSmoke"></u-switch>
          </view>
        </u-form-item>
      </view>
      <u-gap :height="$theme.cm_section_top_gap_height" v-show="objInfo.checkMode == M_CH_MODE.SECRET_VISIT" :bg-color="$theme.cm_gap_color"></u-gap>
      <form-section v-show="objInfo.checkMode == M_CH_MODE.SECRET_VISIT" @rightTap="addGoodTap" title="购买记录" :customStyle="customStyle" rightIcon="plus-circle" :rightIconSize="40"></form-section>
      <view class="info-lay u-m-b-30" v-show="objInfo.checkMode == M_CH_MODE.SECRET_VISIT">
        <view class="card" v-for="(item, index) in buys" :key="index">
          <clientItem :client="item" :id="index" @proList="proList"></clientItem>
        </view>
      </view>
    </view>
    <view class="submit-btns btn-other-cls">
      <u-button plain class="u-btn" type="primary" @click="savePlan(0)">
        保存
      </u-button>
      <u-button class="u-btn" type="primary" @click="savePlan(1)">
        提交
      </u-button>
    </view>
    <u-popup mode="bottom" height="844rpx" v-model="brandShow">
      <view class="content" style="padding: 40rpx;">
        <view style="padding-bottom: 18rpx;">
          <u-form :model="orgForm" label-width="140" label-position="top" :border-bottom="false" :label-style="labelStyle">
            <u-row>
              <u-col span="12">
                <u-form-item :border-bottom="false" style="font-weight:900;">
                  {{ brandObj.productName }}
                </u-form-item>
              </u-col>
            </u-row>
            <u-row>
              <u-col span="6">
                <u-form-item label="单价：" :border-bottom="false">
                  {{ brandObj.price }}
                </u-form-item>
              </u-col>
              <u-col span="6">
                <u-form-item label="数量(条)：" :border-bottom="false">
                  {{ brandObj.qty }}
                </u-form-item>
              </u-col>
            </u-row>
            <u-line color="#dfe0e1"></u-line>
            <u-row>
              <u-col span="6">
                <u-form-item label="金额：" :border-bottom="false">
                  {{ brandObj.money }}
                </u-form-item>
              </u-col>
              <u-col span="6">
                <u-form-item label="考核结果：" :border-bottom="false">
                  {{ $u.dic.getDicNameByKeyCode($u.dic.DIC_KEYS.M_SIMPLING_RESULT_TYPE,brandObj.checkresult) }}
                </u-form-item>
              </u-col>
            </u-row>
            <u-line color="#dfe0e1"></u-line>
            <u-row>
              <u-col span="12">
                <u-form-item label="样本编号：" :border-bottom="false">
                  {{ $u.test.isEmpty(brandObj.seqnum) ? '/' : brandObj.seqnum }}
                </u-form-item>
              </u-col>
            </u-row>
          </u-form>
        </view>
        <u-line color="#dfe0e1"></u-line>
        <view class="btn">
          <u-button size="default" type="error" plain @click="delGoodsItem">
            <text>删除记录</text>
            <u-toast ref="uToast" />
          </u-button>
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script>
import formSection from "@/components/form-section.vue";
import checkCheckService from "@/service/check/check.service";
import {ZDictRadio,} from '@/components';
import { M_CH_MODE, M_SIMPLING_RESULT_TYPE, } from "@/service/check/mapping/enum.js";
import clientItem from "./components/clientItem.vue";
export default{
  components:{
    formSection,
    ZDictRadio,
    clientItem,
  },
  onLoad(opt) {
    let obj = this.getRouteParam();
    if(obj.checkUuid){
      // this.$set(this.objInfo,"checkMode","01")
      this.init(obj.checkUuid)
    }else{
      this.objInfo = obj;
      this.$set(this.objInfo,"checkMode",M_CH_MODE.OBSERVATION)
    }
    console.log(this.objInfo);
    // this.init(this.objInfo.checkUuid)
  },
  data(){
    return {
      customStyle:{
        padding:"20rpx 32rpx",
      },
      checkInfo:{
        beCheckedOrg:"",
      },
      goodList:[],
      objInfo:{
        isPublicSale:false,
        isFakeSmoke:false,
        isSmuggleSmoke:false,
        isFfltSmoke:false,
      }, // 回显数据
      buys:[], // 卷烟数组
      brandShow:false,
      brandObj:{},
      labelStyle:{
        'color':"#666666",
        'font-size':'13px',
      },
      buyIndex:0, // 删除所选品规
      orgForm:{},
      M_SIMPLING_RESULT_TYPE: this.$u.dic.getDicByKeyCode(this.$u.dic.DIC_KEYS.M_SIMPLING_RESULT_TYPE),   // 获取字典
      M_CHECK_MODE: this.$u.dic.getDicByKeyCode(this.$u.dic.DIC_KEYS.M_CHECK_MODE),   // 检查模式
      M_CH_MODE:M_CH_MODE,
    };
  },
  methods:{
    addGoodTap(){
      this.setRouteParam(this.buys);
      this.$u.route("pages/mprEvaluation/components/addGoodsInfo?taskSubject=" + this.objInfo.taskSubject);
    },
    init(id){
      checkCheckService.etailSamplingPlan({checkUuid:id,}).then(res => {
        if (res.success) {
          console.log(this.objInfo)
          this.objInfo = res.bean;
          console.log(this.objInfo);
          this.buys = res.bean.buys;
          this.objInfo.isPublicSale = this.objInfo.isPublicSale == 0 ? false : true;
          this.objInfo.isFakeSmoke = this.objInfo.isFakeSmoke == 0 ? false : true;
          this.objInfo.isSmuggleSmoke = this.objInfo.isSmuggleSmoke == 0 ? false : true;
          this.objInfo.isFfltSmoke = this.objInfo.isFfltSmoke == 0 ? false : true;
        }
      })
    },
    // 明察暗访类型过滤处理
    extFilterFun(itm,keyWord){
      if(keyWord == this.$u.dic.DIC_KEYS.M_CHECK_MODE){
        return itm.bcbCode != M_CH_MODE.RANDOM_SECRET;
      }
      return true;
    },
    proList(itm,index){
      console.log(itm)
      this.buyIndex = index;
      this.brandObj = itm;
      this.brandShow = true;
    },
    updateBackState (param) {
      this.buys = param.goodData;
    },
    delGoodsItem(){
      this.buys.splice(this.buyIndex,1);
      this.brandShow = false;
    },
    backPlan(){
      this.backPrev({
        paramKey:'backRefresh',
        param:true,
      })
    },
    savePlan(type){
      let param = {
        ...this.objInfo,
        buys:[...this.buys,],
      }
      // 如果是 固定明察 不传 卷烟购买记录
      if(this.M_CHECK_MODE[0].bcbCode == param.checkMode){
        param.buys = [];
      }else{
        if(!param.buys.length) return this.$u.toast("请添加购买记录!");
      }
      if(param.isPublicSale){
        param.isPublicSale = 1;
      }else{
        param.isPublicSale = 0;
      }
      if(param.isFakeSmoke){
        param.isFakeSmoke = 1;
      }else{
        param.isFakeSmoke = 0;
      }
      if(param.isSmuggleSmoke){
        param.isSmuggleSmoke = 1;
      }else{
        param.isSmuggleSmoke = 0;
      }
      if(param.isFfltSmoke){
        param.isFfltSmoke = 1;
      }else{
        param.isFfltSmoke = 0;
      }
      if (this.M_CHECK_MODE[0].bcbCode != param.checkMode) {
        param.isFakeSmoke = 0;
        param.isSmuggleSmoke = 0;
        param.isFfltSmoke = 0;
        let resultMap = { // 质检结果 转换
          [M_SIMPLING_RESULT_TYPE.FAKE_SMOKE]:'isFakeSmoke',
          [M_SIMPLING_RESULT_TYPE.SMUGGLE_SMOKE]:'isFfltSmoke', //  isFfltSmoke
          [M_SIMPLING_RESULT_TYPE.FZGQD_SMOKE]:'isSmuggleSmoke', // isSmuggleSmoke
        };
        if(param.buys.length){
          param.buys.map((item) => {
            let key = resultMap[item.checkresult];
            if(key){
              param[key] = '1';
            }
          });
        }
      }
      param.status = type == 1 ? "1" : "0"
      this.$modal.showLoading('正在保存...');
      checkCheckService.saveSamplingPlan(param).then(res => {
        if (res.success) {
          this.$modal.hideLoading();
          this.backPrev({
            paramKey:'refresh',
            param:true,
          })
        }else{
          this.$u.toast(res.msg);
        }
      }).catch(err => {
        this.$modal.hideLoading();
        this.$u.toast("检查户数超过计划户数，无法录入!");
        console.log(err)
      });
    },
  },
}
</script>

<style scoped lang="scss">
.page{
  padding: 0;
}
  .pb-60{
    padding-bottom: 120rpx;
  }
	.info-title{
		font-size: 26rpx;
		width: 100%;
		margin-top: 10rpx;
		margin-bottom: 10rpx;
		color: $cm-form-item-label-color;
	}
  .sp-info-value{
    font-size: 20rpx;
		color: $u-main-color;
		font-weight: 400;
		margin-top: 10rpx;
		margin-bottom: 10rpx;
  }
	.info-value{
		font-size: 28rpx;
		color: $u-main-color;
		font-weight: 400;
		margin-top: 10rpx;
		margin-bottom: 10rpx;
	}
	.info-lay{
		padding: $cm-conent-padding-column $cm-content-paddng-row;
	}
	.switch-lay{
		width: 100%;
		display: flex;
		flex-direction: row;
		justify-content: flex-end;
	}
	/deep/.u-td{
		height: auto !important;
	}
	.btn-lay{
		width: 100%;
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-content: center;
		align-items: center;
		padding: 32rpx 0;
		uni-button{
			flex: 0.4;
		}
	}
  .query-btn {
    display: flex;
    position: fixed;
    width: 95%;
    z-index: 999;
    left: 9px;
    bottom: var(--window-bottom, 0);

    .u-btn {
        width: 100%;
    }
}
  .btn{
  position: fixed;
	bottom: 62rpx;
	margin: auto;
	left: 60rpx;
	right: 60rpx;
  z-index: 100;
  background-color: #ffffff;
}
.card{
  margin: 14rpx -12rpx 18rpx -12rpx;
}
</style>
